package fonkin.com.wifisignapp.acticity.sign;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.sundeepk.compactcalendarview.CompactCalendarView;
import com.github.sundeepk.compactcalendarview.domain.Event;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.bean.vo.SignRecordVo;
import fonkin.com.wifisignapp.constants.AppConstants;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RecordView extends AppCompatActivity {

    TextView monthInfor;
    ImageView weekRecord,signStatus;
    TextView startTime,endTime,submitBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_view);
        monthInfor = (TextView)findViewById(R.id.month_infor);
        monthInfor.setText(getDataDetail(new Date()).toString());
        submitBtn = (TextView)findViewById(R.id.submit_btn);
        submitBtn.setVisibility(View.GONE);
        signStatus = (ImageView)findViewById(R.id.sign_status);
        startTime = (TextView)findViewById(R.id.begin_time);
        endTime = (TextView)findViewById(R.id.end_time);
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFm.format(new Date());
        initSignRecord(format);
        final CompactCalendarView compactCalendarView = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
        // Set first day of week to Monday, defaults to Monday so calling setFirstDayOfWeek is not necessary
        // Use constants provided by Java Calendar class
        compactCalendarView.setFirstDayOfWeek(Calendar.MONDAY);

        compactCalendarView.setUseThreeLetterAbbreviation(true);
        // Add event 1 on Sun, 07 Jun 2015 18:20:51 GMT
        Event ev1 = new Event(Color.GREEN, 1433701251000L, "Some extra data that I want to store.");
        compactCalendarView.addEvent(ev1);

        // Added event 2 GMT: Sun, 07 Jun 2015 19:10:51 GMT
        Event ev2 = new Event(Color.GREEN, 1433704251000L);
        compactCalendarView.addEvent(ev2);

        // Query for events on Sun, 07 Jun 2015 GMT.
        // Time is not relevant when querying for events, since events are returned by day.
        // So you can pass in any arbitary DateTime and you will receive all events for that day.
        List<Event> events = compactCalendarView.getEvents(1433701251000L); // can also take a Date object

       // ShowToast.ShowToastShort("Events: " + events,RecordView.this);
        // events has size 2 with the 2 events inserted previously


        // define a listener to receive callbacks when certain events happen.
        compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
            @Override
            public void onDayClick(Date dateClicked) {
               // List<Event> events = compactCalendarView.getEvents(dateClicked);
               // ShowToast.ShowToastShort("Day was clicked: " + dateClicked + " with events " + events,RecordView.this);
                SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
                String format = dateFm.format(dateClicked);
                initSignRecord(format);
            }

            @Override
            public void onMonthScroll(Date firstDayOfNewMonth) {
                String dataDetail = getDataDetail(firstDayOfNewMonth);
                SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
                String format = dateFm.format(firstDayOfNewMonth);
                initSignRecord(format);
                monthInfor.setText(dataDetail);

            }
        });
    }

    public String getDataDetail(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        int day=calendar.get(Calendar.DAY_OF_MONTH);
        return year+"年"+month+"月";
    }

    public void initSignRecord(String dateTime){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(RecordView.this);
        BaseRetrofit.GetBaseRetrofit().getSignByDate(dateTime,preferences.getString("user_id","")).enqueue(new Callback<ReturnVo<SignRecordVo>>() {
            @Override
            public void onResponse(Call<ReturnVo<SignRecordVo>> call, Response<ReturnVo<SignRecordVo>> response) {
                if (response.isSuccessful()){
                    Integer code = response.body().getCode();
                    if (code.equals(AppConstants.SUCCESS)){
                        submitBtn.setVisibility(View.GONE);
                        signStatus.setImageResource(R.drawable.success_sign);
                        startTime.setText("签到时间："+response.body().getData().getStartTimeStr().toString());
                        endTime.setText("签退时间："+response.body().getData().getEndTimeStr().toString());
                    }else if(code.equals(AppConstants.UNKNOW_DATA)){
                        submitBtn.setVisibility(View.GONE);
                        signStatus.setImageResource(R.drawable.un_get_time);
                        startTime.setText("客官别急！");
                        endTime.setText("还没到时间");
                    }else if(code.equals(AppConstants.UNSIGNIN)){
                        submitBtn.setVisibility(View.VISIBLE);
                        signStatus.setImageResource(R.drawable.sign_error);
                        startTime.setText("哎呀呀！");
                        endTime.setText("忘记签到");
                    }else if(code.equals(AppConstants.UNSIGNOUT)){
                        submitBtn.setVisibility(View.VISIBLE);
                        signStatus.setImageResource(R.drawable.sign_error);
                        startTime.setText("哎呀呀！");
                        endTime.setText("忘记签退");
                    }else{
                        submitBtn.setVisibility(View.VISIBLE);
                        signStatus.setImageResource(R.drawable.un_sign);
                        startTime.setText("哎呀呀！");
                        endTime.setText("考勤异常");
                    }
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<SignRecordVo>> call, Throwable t) {

            }
        });
    }

    public void submitError(View v){
        Intent intent = new Intent(RecordView.this,ErrorApply.class);
        startActivity(intent);
    }


}
